package com.ls.x.dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ls.x.activity.beans.VoiceBean;
import com.ls.x.tool.DatabaseHelper;

public class VoiceDao implements IVoiceDao {
	private DatabaseHelper databaseHelper;
	private Cursor cursor = null;

	// 获取VoiceDao实例的时候，提供如下构造方法，该方法需要传入一个DatabaseHelper对象
	public VoiceDao(DatabaseHelper databaseHelper) {
		super();
		this.databaseHelper = databaseHelper;
	}

	private SQLiteDatabase db = null;

	@Override
	public boolean insertVoice(VoiceBean voiceBean) {
		// TODO Auto-generated method stub
		db.execSQL("insert into voice(voice_path,voice_note,context_id) values('"
				+ voiceBean.getVoicePath()
				+ "','"
				+ voiceBean.getVoiceNote()
				+ "'," + voiceBean.getContextId() + ")");
		return true;
	}

	@Override
	public boolean deleteVoiceByPath(String voicePath) {
		// TODO Auto-generated method stub
		db.execSQL("delete from voice where voice_path =" + voicePath + ")");
		return true;
	}

	@Override
	public boolean updateVoiceNoteByPath(String voiceNote, String voicePath) {
		// TODO Auto-generated method stub
		db.execSQL("update voice set voice_note='" + voiceNote
				+ "' where voice_path =" + voicePath + ")");
		return true;
	}

	@Override
	public VoiceBean selectVoiceByContextId(Long contextId) {
		// TODO Auto-generated method stub
		VoiceBean voiceBean = new VoiceBean();
		cursor = db.rawQuery("select * from voice where context_id="
				+ contextId, null);
		if (cursor.moveToNext()) {
			voiceBean.setContextId(contextId);
			voiceBean.setVoicePath(cursor.getString(cursor
					.getColumnIndex("voice_path")));
			voiceBean.setVoiceNote(cursor.getString(cursor
					.getColumnIndex("voice_note")));
			voiceBean.setId(Integer.parseInt(cursor.getString(cursor
					.getColumnIndex("id"))));
			return voiceBean;
		}
		return null;
	}
}
